<?php

namespace app\models;

use app\models\db\MainPage;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\db\Goods;
use yii\db\Query;

/**
 * GoodsQuery represents the model behind the search form about `\app\models\db\Goods`.
 */
class GoodsQuery extends Goods
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'code', 'name', 'display_name', 'subtitle', 'short_name', 'summary', 'unit', 'create_time', 'modify_time', 'shelve_datetime', 'audit_datetime', 'audit_remark', 'mini_programs_id', 'goods_brand_name_id'], 'safe'],
            [['stock', 'is_enabled', 'is_shelved', 'is_recommend', 'is_newest', 'is_hot', 'is_free_express', 'is_promotion', 'is_top', 'is_show', 'is_audit', 'sell_count', 'collected_count', 'is_invoice', 'is_GGCT', 'is_seven_day', 'is_guarantee', 'is_return_goods'], 'integer'],
            [['settlement_price', 'selling_price', 'old_price', 'express_money'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }


    public static function getMainGoodsList($mini_program_id){
        $main_page = MainPage::findOne($mini_program_id);
        $sort = [$main_page->free_express_goods_sort,$main_page->hot_goods_sort,
            $main_page->newest_goods_sort,$main_page->promotion_goods_sort,$main_page->recommend_goods_sort];
        rsort($sort);

        $query = [];

        foreach ($sort as $num){
            if($main_page->free_express_goods_sort == $num && $num !=0){
                $query1 = (new Query())
                    ->select(['t1.id','t1.name as goods_name','t6.name as brand_name','t1.selling_price','t1.sell_count',
                        't1.collected_count','t7.url as image_url'])
                    ->from('t_goods as t1')
                    ->leftJoin('t_goods_brand_name as t6','t6.id=t1.goods_brand_name_id')
                    ->leftJoin('t_goods_image as t5','t5.goods_id=t1.id')
                    ->leftJoin('t_file as t7','t7.id = t5.file_id')
                    ->andWhere('t1.mini_programs_id=:mini_programs_id',[':mini_programs_id'=>$mini_program_id])
                    ->andWhere(['is_enabled' => 1,'is_shelved' => 1,'is_free_express' => 1])
                    ->andWhere(['or','t5.`type`=1',['or','isnull(t5.`type`)']])
                    ->offset(0)
                    ->limit($main_page->free_express_goods_num)
                    ->all();
                if(!empty($query1))
                array_push($query,["name"=>"包邮商品","is_hidden"=>false,"query_name"=>"is_free_express","goods_list"=>$query1]);
            }

            if($main_page->recommend_goods_sort == $num && $num !=0){
                $query1 = (new Query())
                    ->select(['t1.id','t1.name as goods_name','t6.name as brand_name','t1.selling_price',
                        't1.sell_count','t1.collected_count','t7.url as image_url'])
                    ->from('t_goods as t1')
                    ->leftJoin('t_goods_brand_name as t6','t6.id=t1.goods_brand_name_id')
                    ->leftJoin('t_goods_image as t5','t5.goods_id=t1.id')
                    ->leftJoin('t_file as t7','t7.id = t5.file_id')
                    ->andWhere('t1.mini_programs_id=:mini_programs_id',[':mini_programs_id'=>$mini_program_id])
                    ->andWhere(['is_enabled' => 1,'is_shelved' => 1,'is_recommend' => 1])
                    ->andWhere(['or','t5.`type`=1',['or','isnull(t5.`type`)']])
                    ->offset(0)
                    ->limit($main_page->free_express_goods_num)
                    ->all();
                if(!empty($query1))
                    array_push($query,["name"=>"推荐商品","is_hidden"=>false,"query_name"=>"is_recommend","goods_list"=>$query1]);
            }

            if($main_page->promotion_goods_sort == $num && $num !=0){
                $query1 = (new Query())
                    ->select(['t1.id','t1.name as goods_name','t6.name as brand_name','t1.selling_price','t1.sell_count',
                        't1.collected_count','t7.url as image_url'])
                    ->from('t_goods as t1')
                    ->leftJoin('t_goods_brand_name as t6','t6.id=t1.goods_brand_name_id')
                    ->leftJoin('t_goods_image as t5','t5.goods_id=t1.id')
                    ->leftJoin('t_file as t7','t7.id = t5.file_id')
                    ->andWhere('t1.mini_programs_id=:mini_programs_id',[':mini_programs_id'=>$mini_program_id])
                    ->andWhere(['is_enabled' => 1,'is_shelved' => 1,'is_promotion' => 1])
                    ->andWhere(['or','t5.`type`=1',['or','isnull(t5.`type`)']])
                    ->offset(0)
                    ->limit($main_page->promotion_goods_num)
                    ->all();
                if(!empty($query1))
                    array_push($query,["name"=>"促销商品","is_hidden"=>false,"query_name"=>"is_promotion","goods_list"=>$query1]);
            }

            if($main_page->newest_goods_sort == $num && $num !=0){
                $query1 = (new Query())
                    ->select(['t1.id','t1.name as goods_name','t6.name as brand_name','t1.selling_price','t1.sell_count',
                        't1.collected_count','t7.url as image_url'])
                    ->from('t_goods as t1')
                    ->leftJoin('t_goods_brand_name as t6','t6.id=t1.goods_brand_name_id')
                    ->leftJoin('t_goods_image as t5','t5.goods_id=t1.id')
                    ->leftJoin('t_file as t7','t7.id = t5.file_id')
                    ->andWhere('t1.mini_programs_id=:mini_programs_id',[':mini_programs_id'=>$mini_program_id])
                    ->andWhere(['is_enabled' => 1,'is_shelved' => 1,'is_newest' => 1])
                    ->andWhere(['or','t5.`type`=1',['or','isnull(t5.`type`)']])
                    ->offset(0)
                    ->limit($main_page->newest_goods_num)
                    ->all();
                if(!empty($query1))
                    array_push($query,["name"=>"最新商品","is_hidden"=>false,"query_name"=>"is_newest","goods_list"=>$query1]);
            }

            if($main_page->hot_goods_sort == $num && $num !=0){
//                var_dump($main_page->hot_goods_num);
                $query1 = (new Query())
                    ->select(['t1.id','t1.name as goods_name','t6.name as brand_name','t1.selling_price','t1.sell_count',
                        't1.collected_count','t7.url as image_url'])
                    ->from('t_goods as t1')
                    ->leftJoin('t_goods_brand_name as t6','t6.id=t1.goods_brand_name_id')
                    ->leftJoin('t_goods_image as t5','t5.goods_id=t1.id')
                    ->leftJoin('t_file as t7','t7.id = t5.file_id')
                    ->andWhere('t1.mini_programs_id=:mini_programs_id',[':mini_programs_id'=>$mini_program_id])
                    ->andWhere(['is_enabled' => 1,'is_shelved' => 1,'is_hot' => 1])
                    ->andWhere(['or','t5.`type`=1',['or','isnull(t5.`type`)']])
                    ->offset(0)
                    ->limit($main_page->hot_goods_num)
                    ->all();
//                var_dump($query1->rawSql);
                if(!empty($query1))
                    array_push($query,["name"=>"热销商品","is_hidden"=>false,"query_name"=>"is_hot","goods_list"=>$query1]);
            }
        }

        return $query;

    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Goods::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'stock' => $this->stock,
            'settlement_price' => $this->settlement_price,
            'selling_price' => $this->selling_price,
            'old_price' => $this->old_price,
            'create_time' => $this->create_time,
            'modify_time' => $this->modify_time,
            'is_enabled' => $this->is_enabled,
            'is_shelved' => $this->is_shelved,
            'shelve_datetime' => $this->shelve_datetime,
            'is_recommend' => $this->is_recommend,
            'is_newest' => $this->is_newest,
            'is_hot' => $this->is_hot,
            'is_free_express' => $this->is_free_express,
            'is_promotion' => $this->is_promotion,
            'is_top' => $this->is_top,
            'is_show' => $this->is_show,
            'is_audit' => $this->is_audit,
            'audit_datetime' => $this->audit_datetime,
            'sell_count' => $this->sell_count,
            'collected_count' => $this->collected_count,
            'express_money' => $this->express_money,
            'is_invoice' => $this->is_invoice,
            'is_GGCT' => $this->is_GGCT,
            'is_seven_day' => $this->is_seven_day,
            'is_guarantee' => $this->is_guarantee,
            'is_return_goods' => $this->is_return_goods,
        ]);

        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'display_name', $this->display_name])
            ->andFilterWhere(['like', 'subtitle', $this->subtitle])
            ->andFilterWhere(['like', 'short_name', $this->short_name])
            ->andFilterWhere(['like', 'summary', $this->summary])
            ->andFilterWhere(['like', 'unit', $this->unit])
            ->andFilterWhere(['like', 'audit_remark', $this->audit_remark])
            ->andFilterWhere(['like', 'mini_programs_id', $this->mini_programs_id])
            ->andFilterWhere(['like', 'goods_brand_name_id', $this->goods_brand_name_id]);

        return $dataProvider;
    }
}
